<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 平台资金明细
 *
 * @icon fa fa-circle-o
 */
class MemberMoneyRecord extends Backend
{

    /**
     * Record模型对象
     * @var \app\common\model\PlatformMoneyRecord
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\MoneyRecord();

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);

            $list = \app\common\model\MoneyRecord::alias('money_record')
                ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname,order.order_sn')
                ->join('fa_member member','money_record.member_id=member.id','left')
                ->join('fa_order order','money_record.order_id=order.id','left')
                ->join('fa_shop shop','order.shop_id=shop.id','left')
                ->join('fa_agent agent','order.agent_id=agent.id','left')
                ->where($where)
                ->where(['money_record.agent_id'=>0,'money_record.shop_id'=>0])
                ->order('money_record.id desc')
                ->paginate($limit);
//            halt($list);







//            $list = $this->model->with(['member','orders'])->where($where)->where(['money_record.agent_id'=>0,'money_record.shop_id'=>0])->order($sort, $order)->paginate($limit)->each(function ($item){
//                $item['shop_name'] = '';
//                $item['agent_name'] = '';
//                if (!empty($item['orders'])){
//                    $item['shop_name'] = \app\common\model\Shop::where('id',$item['orders']['shop_id'])->value('shop_name');
//                    $item['agent_name'] = \app\common\model\Agent::where('id',$item['orders']['agent_id'])->value('nickname');
//                }
//            });
            $count = [];
            if (empty($filter['detail_type'])){ //全统计
                $chonzhi = \app\common\model\MoneyRecord::alias('money_record')
                    ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                    ->join('fa_member member','money_record.member_id=member.id','left')
                    ->join('fa_order order','money_record.order_id=order.id','left')
                    ->join('fa_shop shop','order.shop_id=shop.id','left')
                    ->join('fa_agent agent','order.agent_id=agent.id','left')
                    ->where($where)
                    ->where(['money_record.detail_type'=>1,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                    ->sum('money_record.money'); //充值

                $tixian = \app\common\model\MoneyRecord::alias('money_record')
                    ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                    ->join('fa_member member','money_record.member_id=member.id','left')
                    ->join('fa_order order','money_record.order_id=order.id','left')
                    ->join('fa_shop shop','order.shop_id=shop.id','left')
                    ->join('fa_agent agent','order.agent_id=agent.id','left')
                    ->where($where)
                    ->where(['money_record.detail_type'=>2,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                    ->sum('money_record.money');
                $tixian_tk = \app\common\model\MoneyRecord::alias('money_record')
                    ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                    ->join('fa_member member','money_record.member_id=member.id','left')
                    ->join('fa_order order','money_record.order_id=order.id','left')
                    ->join('fa_shop shop','order.shop_id=shop.id','left')
                    ->join('fa_agent agent','order.agent_id=agent.id','left')
                    ->where($where)
                    ->where(['money_record.detail_type'=>7,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                    ->sum('money_record.money');
                $tixian = $tixian - $tixian_tk;

                $xiaofei = \app\common\model\MoneyRecord::alias('money_record')
                    ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                    ->join('fa_member member','money_record.member_id=member.id','left')
                    ->join('fa_order order','money_record.order_id=order.id','left')
                    ->join('fa_shop shop','order.shop_id=shop.id','left')
                    ->join('fa_agent agent','order.agent_id=agent.id','left')
                    ->where($where)
                    ->where(['money_record.detail_type'=>4,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                    ->sum('money_record.money');

                $tuikuan = \app\common\model\MoneyRecord::alias('money_record')
                    ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                    ->join('fa_member member','money_record.member_id=member.id','left')
                    ->join('fa_order order','money_record.order_id=order.id','left')
                    ->join('fa_shop shop','order.shop_id=shop.id','left')
                    ->join('fa_agent agent','order.agent_id=agent.id','left')
                    ->where($where)
                    ->where(['money_record.detail_type'=>5,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                    ->sum('money_record.money'); //充值


                $members = \app\common\model\MoneyRecord::alias('money_record')
                    ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                    ->join('fa_member member','money_record.member_id=member.id','left')
                    ->join('fa_order order','money_record.order_id=order.id','left')
                    ->join('fa_shop shop','order.shop_id=shop.id','left')
                    ->join('fa_agent agent','order.agent_id=agent.id','left')
                    ->where($where)
                    ->where(['money_record.agent_id'=>0,'money_record.shop_id'=>0])
                    ->group('money_record.member_id')
                    ->column('money_record.member_id');

                $all_money = \app\common\model\Member::whereIn('id',$members)->sum('balance');


//                $chonzhi = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>1,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money'); //充值
//                $tixian = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>2,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money');//提现
//                $xiaofei = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>4,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money');//消费
//                $tuikuan = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>5,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money');//退款
                $count = [['name'=>'累计充值','value'=>$chonzhi],['name'=>'累计提现','value'=>$tixian],['name'=>'累计消费','value'=>$xiaofei],['name'=>'累计退款','value'=>$tuikuan],['name'=>'累计充值余额','value'=>$all_money]];
            }else{ //按类别统计
                $members = \app\common\model\MoneyRecord::alias('money_record')
                    ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                    ->join('fa_member member','money_record.member_id=member.id','left')
                    ->join('fa_order order','money_record.order_id=order.id','left')
                    ->join('fa_shop shop','order.shop_id=shop.id','left')
                    ->join('fa_agent agent','order.agent_id=agent.id','left')
                    ->where($where)
                    ->where(['money_record.detail_type'=>$filter['detail_type'],'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                    ->group('money_record.member_id')
                    ->column('money_record.member_id');

                $all_money = \app\common\model\Member::whereIn('id',$members)->sum('balance');

                if ($filter['detail_type'] == 1){
                    $chonzhi = \app\common\model\MoneyRecord::alias('money_record')
                        ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                        ->join('fa_member member','money_record.member_id=member.id','left')
                        ->join('fa_order order','money_record.order_id=order.id','left')
                        ->join('fa_shop shop','order.shop_id=shop.id','left')
                        ->join('fa_agent agent','order.agent_id=agent.id','left')
                        ->where($where)
                        ->where(['money_record.detail_type'=>1,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                        ->sum('money_record.money'); //充值
//                    $chonzhi = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>1,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money'); //充值
                    $count = [['name'=>'累计充值','value'=>$chonzhi],['name'=>'累计提现','value'=>0],['name'=>'累计消费','value'=>0],['name'=>'累计退款','value'=>0],['name'=>'累计充值余额','value'=>$all_money]];
                }elseif ($filter['detail_type'] == 2){
                    $tixian = \app\common\model\MoneyRecord::alias('money_record')
                        ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                        ->join('fa_member member','money_record.member_id=member.id','left')
                        ->join('fa_order order','money_record.order_id=order.id','left')
                        ->join('fa_shop shop','order.shop_id=shop.id','left')
                        ->join('fa_agent agent','order.agent_id=agent.id','left')
                        ->where($where)
                        ->where(['money_record.detail_type'=>2,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                        ->sum('money_record.money');
//                    $tixian = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>2,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money');//提现
                    $count = [['name'=>'累计充值','value'=>0],['name'=>'累计提现','value'=>$tixian],['name'=>'累计消费','value'=>0],['name'=>'累计退款','value'=>0],['name'=>'累计充值余额','value'=>$all_money]];
                }elseif ($filter['detail_type'] == 4){
                    $xiaofei = \app\common\model\MoneyRecord::alias('money_record')
                        ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                        ->join('fa_member member','money_record.member_id=member.id','left')
                        ->join('fa_order order','money_record.order_id=order.id','left')
                        ->join('fa_shop shop','order.shop_id=shop.id','left')
                        ->join('fa_agent agent','order.agent_id=agent.id','left')
                        ->where($where)
                        ->where(['money_record.detail_type'=>4,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                        ->sum('money_record.money');
//                    $xiaofei = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>4,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money');//消费
                    $count = [['name'=>'累计充值','value'=>0],['name'=>'累计提现','value'=>0],['name'=>'累计消费','value'=>$xiaofei],['name'=>'累计退款','value'=>0],['name'=>'累计充值余额','value'=>$all_money]];
                }elseif ($filter['detail_type'] == 5){
                    $tuikuan = \app\common\model\MoneyRecord::alias('money_record')
                        ->field('money_record.*,member.member_name,shop.shop_name,agent.nickname')
                        ->join('fa_member member','money_record.member_id=member.id','left')
                        ->join('fa_order order','money_record.order_id=order.id','left')
                        ->join('fa_shop shop','order.shop_id=shop.id','left')
                        ->join('fa_agent agent','order.agent_id=agent.id','left')
                        ->where($where)
                        ->where(['money_record.detail_type'=>5,'money_record.agent_id'=>0,'money_record.shop_id'=>0])
                        ->sum('money_record.money');
//                    $tuikuan = $this->model->with(['member','orders'])->where($where)->where(['money_record.detail_type'=>5,'money_record.agent_id'=>0,'money_record.shop_id'=>0])->sum('money');//退款
                    $count = [['name'=>'累计充值','value'=>0],['name'=>'累计提现','value'=>0],['name'=>'累计消费','value'=>0],['name'=>'累计退款','value'=>$tuikuan],['name'=>'累计充值余额','value'=>$all_money]];
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>['count'=>$count]);

            return json($result);
        }
        return $this->view->fetch();
    }


}
